package cc.iotkit.manager.service.miniAdmin;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.data.miniAdmin.IAreaData;
import cc.iotkit.data.miniAdmin.IGoodsData;
import cc.iotkit.manager.dto.vo.miniAdmin.MiniAdminAreaVo;
import cc.iotkit.manager.dto.vo.miniAdmin.MiniAdminGoodsVo;
import cc.iotkit.manager.service.IGoodsService;
import cc.iotkit.model.miniAdmin.Area;
import cc.iotkit.model.miniAdmin.Goods;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class AreaServiceImpl implements IAreaService {

    private final IAreaData areaData;

    @Override
    public void save(MiniAdminAreaVo goodsVo) {
        Area area = new Area();
        BeanUtils.copyProperties(goodsVo, area);
        areaData.saveArea(area);
    }

    @Override
    public void update(MiniAdminAreaVo goodsVo) {
        Area goods = new Area();
        BeanUtils.copyProperties(goodsVo,goods);
        areaData.updateArea(goods);
    }

    @Override
    public Paging<MiniAdminAreaVo> getPageList(PageRequest<MiniAdminAreaVo> query) {
        Paging pageing = new Paging<>();

        pageing = areaData.findByConditions(query.to(Area.class));

        List<Area> goodList =  pageing.getRows();
        List<MiniAdminAreaVo> goodsVos = new ArrayList<>();

        for (Area goods:goodList){
            MiniAdminAreaVo goodsVo = new MiniAdminAreaVo();
            BeanUtils.copyProperties(goods,goodsVo);
            goodsVos.add(goodsVo);
        }
        pageing.setRows(goodsVos);
         return  pageing;
    }


    @Override
    public void delete(Long id) {
        areaData.deleteArea(id);
    }
}
